package org.sundatagroup.aurora.core.entity.model;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.sundatagroup.aurora.core.entity.pojo.Project;

import java.io.Serializable;
import java.util.List;

/**
 * <p>模块 {@link Project} 在数据库的数据结构
 * </p>
 * 
 * @version 0.1.1
 * <br>补充 texts 和 desc 字段
 * 
 * @author Zdream
 * @date 2018-04-02
 * @since 0.1
 */
@Document(collection = "project")
public class ProjectModel implements Serializable {
	
	private static final long serialVersionUID = -6897442150152416349L;
	
	@Id
	private int pid;
	private String name;
	private List<Integer> modules;
	private String options;
	private String texts;
	private String desc;
	
	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Integer> getModules() {
		return modules;
	}

	public void setModules(List<Integer> modules) {
		this.modules = modules;
	}

	public String getOptions() {
		return options;
	}
	
	public JsonObject getOptionToJson() {
		if (options == null) {
			return null;
		}
		JsonParser parser = new JsonParser();
		return parser.parse(options).getAsJsonObject();
	}

	public void setOptions(String options) {
		this.options = options;
	}

	public void setOptions(JsonObject options) {
		if (options == null) {
			this.options = null;
		} else {
			this.options = options.toString();
		}
	}

	public String getTexts() {
		return texts;
	}
	
	public JsonArray getTextToJson() {
		if (texts == null) {
			return null;
		}
		JsonParser parser = new JsonParser();
		return parser.parse(texts).getAsJsonArray();
	}

	public void setTexts(String texts) {
		this.texts = texts;
	}

	public void setTexts(JsonArray texts) {
		if (texts == null) {
			this.texts = null;
		} else {
			this.texts = texts.toString();
		}
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	@Override
	public String toString() {
		return "Project{" +
				"pid=" + pid +
				", name='" + name + '\'' +
				", modules='" + modules + '\'' +
				'}';
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + pid;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProjectModel other = (ProjectModel) obj;
		if (pid != other.pid)
			return false;
		return true;
	}

}
